package com.dog.basics.service.mtkj.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.lang.Validator;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dog.basics.bean.dto.admin.mtkj.HospitalCostAddDto;
import com.dog.basics.bean.dto.admin.mtkj.HospitalCostEditDto;
import com.dog.basics.bean.dto.admin.mtkj.HospitalCostFromDto;
import com.dog.basics.bean.model.mtkj.MtDeviceHospital;
import com.dog.basics.bean.model.mtkj.MtDeviceHospitalCost;
import com.dog.basics.bean.vo.admin.mtkj.HospitalCostVo;
import com.dog.basics.bean.vo.api.mtkj.ApiHospitalCostVo;
import com.dog.basics.common.util.PageParam;
import com.dog.basics.dao.mtkj.MtDeviceHospitalCostMapper;
import com.dog.basics.dao.mtkj.MtDeviceHospitalMapper;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.service.mtkj.IMtDeviceHospitalCostService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 医院费用 服务实现类
 * </p>
 *
 * @author wph
 * @since 2020-09-29
 */
@Service
@AllArgsConstructor
public class MtDeviceHospitalCostServiceImpl extends ServiceImpl<MtDeviceHospitalCostMapper, MtDeviceHospitalCost> implements IMtDeviceHospitalCostService {
    private final MtDeviceHospitalMapper mtDeviceHospitalMapper;


    /**
     * 分页的条件公用类
     *
     * @param name,mobile
     * @param hospitalIds
     */
    public List<Long> gethospitalIdsWhere(String name, List<Long> hospitalIds) {
        List<Long> collect = mtDeviceHospitalMapper.selectList(Wrappers.<MtDeviceHospital>lambdaQuery().like(StrUtil.isNotBlank(name), MtDeviceHospital::getHospitalName, name)).stream().map(e -> e.getId()).collect(Collectors.toList());
        if (CollUtil.isNotEmpty(collect)) {
            hospitalIds = collect;
        } else {
            hospitalIds.add(-2L);
        }
        return hospitalIds;
    }

    /**
     * @Description: 分页医院费用
     * @Param: status
     * @return: IPage<HospitalCostVo>
     * @Author: wph
     * @Date:
     */
    @Override
    public IPage<HospitalCostVo> getPageList(PageParam param, HospitalCostFromDto obj) {
        // 判断所属医院ID
        List<Long> hospitalIds = CollUtil.newArrayList();
        if (StrUtil.isNotBlank(obj.getHospitalName())) {
            hospitalIds = gethospitalIdsWhere(obj.getHospitalName(), hospitalIds);
        }
        // 查询数据信息
        this.page(param, Wrappers.<MtDeviceHospitalCost>lambdaQuery()
                .in(CollUtil.isNotEmpty(hospitalIds), MtDeviceHospitalCost::getDeviceHospitalId, hospitalIds)
                .eq(ObjectUtil.isNotNull(obj.getDeviceHospitalId()), MtDeviceHospitalCost::getDeviceHospitalId, obj.getDeviceHospitalId())
                .orderBy(Boolean.TRUE, obj.getIsOrderByAvg(), MtDeviceHospitalCost::getCreateTime));
        List<MtDeviceHospitalCost> records = param.getRecords();
        // 循环处理格式化数据
        param.setRecords(records.stream().map(hospitalCost -> {
            HospitalCostVo newsVo = BeanUtil.toBean(hospitalCost, HospitalCostVo.class);
            //  查询
            MtDeviceHospital mtDeviceHospital = mtDeviceHospitalMapper.selectById(hospitalCost.getDeviceHospitalId());
            newsVo.setDeviceHospitalName(ObjectUtil.isNotNull(mtDeviceHospital) ? mtDeviceHospital.getHospitalName() : "-");
            return newsVo;
        }).collect(Collectors.toList()));
        return param;
    }

    /**
     * @Description: 医院费用详情
     * @Param: id
     * @return: ApiLockVo
     * @Author: wph
     * @Date:
     */
    @Override
    public ApiHospitalCostVo getApiObjXq(Long id) {
        ApiHospitalCostVo apiHospitalCostVo = new ApiHospitalCostVo();
        MtDeviceHospital mtDeviceHospital = mtDeviceHospitalMapper.selectById(id);
        Validator.validateNotNull(mtDeviceHospital, "医院不存在!");
        List<MtDeviceHospitalCost> mtDeviceHospitalCostList = this.list(Wrappers.<MtDeviceHospitalCost>lambdaQuery().eq(MtDeviceHospitalCost::getDeviceHospitalId, id).orderByDesc(MtDeviceHospitalCost::getCreateTime));
        if (CollUtil.isNotEmpty(mtDeviceHospitalCostList)) {
            BeanUtil.copyProperties(mtDeviceHospitalCostList.get(0), apiHospitalCostVo);
        }
        return apiHospitalCostVo;

    }

    /**
     * @Description: 增加医院费用
     * @Param: obj
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/05/19  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean saveObj(HospitalCostAddDto obj) {
        //判断是否已经存在
        List<MtDeviceHospitalCost> list = this.list(Wrappers.<MtDeviceHospitalCost>lambdaQuery().eq(MtDeviceHospitalCost::getDeviceHospitalId, obj.getDeviceHospitalId()).eq(MtDeviceHospitalCost::getWheeType, obj.getWheeType()));
        Validator.validateFalse(CollUtil.isNotEmpty(list), "只能有一条数据!");
        MtDeviceHospital mtDeviceHospital = mtDeviceHospitalMapper.selectById(obj.getDeviceHospitalId());
        Validator.validateNotNull(mtDeviceHospital, "医院不存在!");
        MtDeviceHospitalCost dogNews = BeanUtil.toBean(obj, MtDeviceHospitalCost.class);
        dogNews.setCreateTime(DateUtil.currentSeconds());
        dogNews.setCreateBy(SecurityUtils.getSysUser().getUserId());
        Validator.validateTrue(this.save(dogNews), "数据保存失败!");
        return Boolean.TRUE;
    }

    /**
     * @Description: 修改医院费用
     * @Param: obj
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/05/19  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean editObj(HospitalCostEditDto obj) {
        MtDeviceHospitalCost dogNews = this.getById(obj.getId());
        Validator.validateNotNull(dogNews, "无此数据!");
        MtDeviceHospital mtDeviceHospital = mtDeviceHospitalMapper.selectById(obj.getDeviceHospitalId());
        Validator.validateNotNull(mtDeviceHospital, "医院不存在!");
        // 判断是否有修改医院标识
        if (!Validator.equal(mtDeviceHospital.getId(), dogNews.getDeviceHospitalId())) {
            List<MtDeviceHospitalCost> deviceHospitalCosts = this.list(Wrappers.<MtDeviceHospitalCost>lambdaQuery().eq(MtDeviceHospitalCost::getDeviceHospitalId, obj.getDeviceHospitalId()).eq(MtDeviceHospitalCost::getWheeType, obj.getWheeType()));
            Validator.validateTrue(CollUtil.isEmpty(deviceHospitalCosts), "修改后医院的收费类型已存在");
        }
        // 判断是否有修改设备类型
        if (!Validator.equal(dogNews.getWheeType(), obj.getWheeType())) {
            List<MtDeviceHospitalCost> deviceHospitalCosts = this.list(Wrappers.<MtDeviceHospitalCost>lambdaQuery().eq(MtDeviceHospitalCost::getDeviceHospitalId, obj.getDeviceHospitalId()).eq(MtDeviceHospitalCost::getWheeType, obj.getWheeType()));
            Validator.validateTrue(CollUtil.isEmpty(deviceHospitalCosts), "修改后医院的收费类型已存在");
        }
        BeanUtil.copyProperties(obj, dogNews);
        dogNews.setUpdateTime(DateUtil.currentSeconds());
        dogNews.setUpdateBy(SecurityUtils.getSysUser().getUserId());
        Validator.validateTrue(this.updateById(dogNews), "修改数据失败!");
        return Boolean.TRUE;
    }

    /**
     * @Description: 删除医院费用
     * @Param: ids
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/05/19  0027
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteObj(Long id) {
        MtDeviceHospitalCost dogNews = this.getById(id);
        Validator.validateNotNull(dogNews, "无此数据!");
        Validator.validateTrue(this.removeById(id), "数据删除失败!");
        return Boolean.TRUE;
    }
}
